﻿#region FileHeader
//////////////////////////////////////////////////////////////////////////////////
// Author:                          Hector Llanos
// Date:                            03.19.2011
// FileName:                        CEnemy.cs
// Reason:                          basic enemy class
//////////////////////////////////////////////////////////////////////////////////
#endregion


using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Linq;
using System.Text;

using Base;
using Run_Game;
using MessagingSystem;
using Event_System;
using Globals;

namespace Objects
{
    public class CEnemy : CBase, IListener
    {
        private float m_fBulletDelay;
        //private float m_fWalkDelay;
        private bool m_bLeader;

        public bool Leader
        {
            get { return m_bLeader; }
            set { m_bLeader = value; }
        }

        public CEnemy() : base(Game1.Instance.Content, "SpriteSheets/player", 8, 1, 10)
        {
            //RegisterClient("Separation", this);
            //CEventSystem.Instance.RegisterClient("PlayerBulletHit", this);

            Type = type.ENEMY;

        }
        ~CEnemy()
        {
            //CEventSystem.Instance.UnregisterClient("PlayerBulletHit", this);
        }
        public  override void Update(float fElapsedTime)
        {
            m_fBulletDelay += fElapsedTime;
            //m_fWalkDelay += fElapsedTime;

            //position
            Velocity = new Vector2((float)CGlobal.Rand.Next() - 5f, (float)CGlobal.Rand.Next() - 5f);

            Animtion.UpdateFrame(fElapsedTime);
            base.Update(fElapsedTime);

            //off screen
            //Game1.Instance.MessageSystem.SendMessage(new CDestoryEnemy(this));
            if (Position.X + Width / 2 > CGlobal.Screenwidth)
                PositionX = 0;

            if (Position.X < 0)
                PositionX = CGlobal.Screenwidth- Width/2;

            if (Position.Y < 0)
                PositionY = CGlobal.Screenheight - Height /2;

            if (Position.Y + Height / 2 > CGlobal.Screenheight)
                PositionY = 0;

            //shooting
            if (m_fBulletDelay >= 0.5f)
            {
                m_fBulletDelay = 0.0f;
                CMessageSystem.Instance.SendMessage(new CCreateBulletMessage(this, type.ENEMYBULLET));
            }

        }
        public override bool CheckCollision(CBase pBase)
        {
            //if the player shoots me
            if (base.CheckCollision(pBase) && pBase.Type == type.BASEBULLET)
            {
                //destroy this
                CMessageSystem.Instance.SendMessage(new CDestoryEnemy(this));

                //destroy the bullet too
                CMessageSystem.Instance.SendMessage(new CDestroyBulletMessage(pBase));
                return true;
            }

            return false;
        }
        public override void Render()
        {
            Animtion.DrawFrame(Game1.Instance.SpriteBatch, Position);
        }

        public void HandleEvent(CEvent pEvent)
        {

        }
       
    }

}
